package cn.wolfcode.service.impl;

import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.mapper.SystemDictionaryMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {
    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;

    @Override
    @Transactional
    public void saveOrUpdate(SystemDictionary systemDictionary) {
        if (systemDictionary.getId() == null) {
            systemDictionaryMapper.insert(systemDictionary);
        } else {
            systemDictionaryMapper.updateByPrimaryKey(systemDictionary);
        }
    }

    @Override
    @Transactional
    public void delete(Long id) {
        if(id != null){
            systemDictionaryMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public SystemDictionary selectOne(Long id) {
        SystemDictionary systemDictionary = systemDictionaryMapper.selectByPrimaryKey(id);
        return systemDictionary;
    }

    @Override
    public List<SystemDictionary> selectAll() {
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectAll();
        return systemDictionarys;
    }

    @Override
    public PageInfo<SystemDictionary> query(QueryObject qo) {
        //开始分页,会拦截下一个查询的sql
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectPage(qo); //会被拦截并加上limit
        return new PageInfo<>(systemDictionarys);
    }
}
